package com.ibsol.control
{
	import com.ibsol.event.EventName;
	import com.ibsol.model.TourPackage;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	public class PackageFilter extends EventDispatcher
	{
		private static var _instance:PackageFilter;
		
		public var packagePrice:int;
		public var packageDuration:int;
		
		/* yijie's code*/
		[Bindable]
		public var packageAC:ArrayCollection;
		public var filters:Array = new Array;
		
		
		
		public function PackageFilter(enforcer:SingletonEnforcer){}
		
		public static function getInstance():PackageFilter
		{
			if(_instance == null)
				_instance = new PackageFilter(new SingletonEnforcer());
			return _instance;
		}
		
		
		public function priceFilter(item:TourPackage):Boolean
		{
			if((item.price - packagePrice) <= 500 && (item.price - packagePrice) >= 0)
				return true;
			if((packagePrice - item.price) <= 500 && (packagePrice - item.price) >= 0)
				return true;
			return false;
		}
		
		public function durationFilter(item:TourPackage):Boolean
		{
			if(item.duration == packageDuration)
				return true;
			else
				return false;
		}
		
		/* yijie's code*/
		
		private function filterFunction(item:Object):Boolean
		{
			var rB:Boolean = true;
			for each(var f:Function in filters)
			{
				rB = rB&&(Boolean)(f(item));
			}
			return rB;
		}
		
		public function getFilteredData():void
		{
			packageAC.filterFunction = filterFunction;
			packageAC.refresh();
			dispatchEvent(new Event(EventName.EVENT_PACKAGE_CHANGED));
		}
		
		public function setFilter(filterName:String,filter:Function):void
		{
			filters[filterName]=filter;
			getFilteredData();
			dispatchEvent(new Event(EventName.EVENT_FILTER_CHANGED));
		}
		

	}
}
/**
 * This class only can be viewed in this class
 */
class SingletonEnforcer{}